/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package freemaker;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author RoderickNestor
 */
public class Freemaker1 {

    public static void main(String[] args) {

        //Freemarker configuration object
        Configuration cfg = new Configuration();
        try {
            cfg.setDirectoryForTemplateLoading(new File("E:\\agile\\Freemaker\\src\\"));
        } catch (IOException ex) {
            Logger.getLogger(Freemaker1.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            //Load template from source folder
            Template template = cfg.getTemplate("helloworld.ftl");
            

            // Build the data-model
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("message", "Hello World!");
            data.put("issentes", "hol");

            //List parsing
            List<String> countries = new ArrayList<String>();
            countries.add("India");
            countries.add("United States");
            countries.add("Germany");
            countries.add("France");

            data.put("countries", countries);

            // Console output
            Writer out = new OutputStreamWriter(System.out);
            template.process(data, out);
            out.flush();

            // File output
            Writer file = new FileWriter(new File("E:\\agile\\Freemaker\\src\\FTL_helloworld.txt"));
            template.process(data, file);
            file.flush();
            file.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
}
